package com.zjm.controller;

import com.zjm.entity.*;
import com.zjm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService service;

    @RequestMapping("/registerStudent.do")
    public ModelAndView registerStudent(Student student,String confirmPassword){
        System.out.println("---------------学生注册--------------");
        ModelAndView mv = new ModelAndView();
        Map<String,Object> map = new HashMap<>();
        System.out.println("学生注册信息："+student);
        map = service.register(student,confirmPassword);
        if(map.get("msg")==null){
            mv.setViewName("ULogin");
            return mv;
        }
        String msg = student.getName()+map.get("msg");
        System.out.println(msg);
        mv.addObject("tips",msg);
        mv.setViewName("URegister");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/addStudentName.do",produces = "text/plain;charset=utf-8")
    public String addStudentName(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        System.out.println("student     "+name);
        return name;
    }

    @ResponseBody
    @RequestMapping("/showMyClas.do")
    public List<Select_clas> showMyClas(HttpServletRequest request){
        Integer student_id = (Integer) request.getSession().getAttribute("id");
        List<Select_clas> clasList = service.myClas(student_id);
        System.out.println("myClas:"+student_id+"    "+clasList);
        return clasList;
    }

    @ResponseBody
    @RequestMapping("/showAllClas.do")
    public List<Clas> showAllClas(HttpServletRequest request){
        Integer student_id = (Integer) request.getSession().getAttribute("id");
        List<Clas> clasList = service.showAllClas(student_id);
        System.out.println("allClas            "+clasList);
        return clasList;
    }


    @RequestMapping("/selectClas.do")
    public ModelAndView selectClas(HttpServletRequest request,Integer clas_id){
        ModelAndView mv = new ModelAndView();
        Integer student_id = (Integer) request.getSession().getAttribute("id");
        System.out.println(clas_id+"        "+student_id);
        service.addClas(clas_id,student_id);
        mv.setViewName("SSelectClas");
        return mv;
    }

    @RequestMapping("/studentVacate.do")
    public ModelAndView studentVacate(Vacate vacate){
        ModelAndView mv = new ModelAndView();
        System.out.println("学生请假："+vacate);
        service.vacate(vacate);
        mv.setViewName("SVacate");
        return mv;
    }


    @ResponseBody
    @RequestMapping("/showStudentVacate.do")
    public List<Vacate> showStudentVacate(HttpServletRequest request){
        System.out.println("---------------请假查看进度--------------");
        Integer student_id = (Integer) request.getSession().getAttribute("id");
        List<Vacate> vacateList = service.myVacates(student_id);
        System.out.println(vacateList);
        return vacateList;
    }

    @RequestMapping("/studentChangeMajor.do")
    public ModelAndView studentChangeMajor(Change_major change_major){
        ModelAndView mv = new ModelAndView();
        System.out.println("学生转专业："+change_major);
        service.changeMajor(change_major);
        mv.setViewName("SChangeMajor");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/showStudentChangeMajor.do")
    public Change_major showStudentChangeMajor(HttpServletRequest request){
        System.out.println("---------------查看转专业进度--------------");
        Integer student_id = (Integer) request.getSession().getAttribute("id");
        System.out.println("studentChangeMajor"+student_id);
        Change_major changeMajor = service.myChangeMajor(student_id);
        System.out.println(changeMajor);
        return changeMajor;
    }
}
